#region Licencia

//   Copyright [2008] Jose Manuel Islas Romero (trentcioran@gmail.com)
//  
//     Licensed under the Apache License, Version 2.0 (the "License");
//     you may not use this file except in compliance with the License.
//     You may obtain a copy of the License at
//  
//         http://www.apache.org/licenses/LICENSE-2.0
//  
//     Unless required by applicable law or agreed to in writing, software
//     distributed under the License is distributed on an "AS IS" BASIS,
//     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//     See the License for the specific language governing permissions and
//     limitations under the License.

#endregion

#region Usings

using System;
using System.Web;

#endregion

namespace Praedator.Framework.Core.Security.Web
{
    public class ContextHttpModule : IHttpModule
    {
        #region Implementation of IHttpModule

        public void Init (HttpApplication context)
        {
            context.BeginRequest += OnBeginRequest;
            context.EndRequest += OnEndRequest;
        }

        public void Dispose ()
        {
        }

        #endregion

        private void OnBeginRequest (object sender, EventArgs eventArgs)
        {
            try
            {
                var application = (HttpApplication) sender;
                var user = application.Session [typeof (IUser).Name] as IUser;

                if (user == null)
                {
                    application.Response.Redirect ("LogIn.aspx", true);
                }
                Context.User = user;
            }
            catch (Exception)
            {
            }
        }

        private void OnEndRequest (object sender, EventArgs eventArgs)
        {
        }
    }
}